﻿using System;
using System.IO;
using System.Text;
using System.Collections.Generic;
namespace DelicateDict.Classes
{
    class Function
    {
        /// <summary>
        /// get substring with first occurrence of a string
        /// return string before or after meeting first occurence
        /// </summary>
        /// <param name="input"></param>
        /// <param name="c"></param>
        /// <param name="firstoccur">true if get string before occurnce</param>
        /// <returns></returns>
        public static string strstr(string input, char c, bool firstoccur)
        {
            int pos = input.IndexOf(c);
            int len = input.Length;
            string output = "";
            if (pos != -1)
            {
                if (firstoccur)
                    output = input.Substring(0, pos);
                else
                    output = input.Substring(pos, len - pos);
            }
            return output;
        }
        // convert int base10 to base64
        public static int Base64ToBase10(string num)
        {
            string opcode = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
            int number = 0;
            int len = num.Length;
            for (int i = 0; i < len; i++)
            {
                number += opcode.IndexOf(num[i]) * (int)Math.Pow(64, len - i - 1);

            }
            return number;
        }
        // return string base64 from int base10
           public static  string Base10ToBase64(int base10)
        {
            string opcode = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
            string tmp = "", base64 = "";

            do
            {
                tmp += opcode[base10 % 64];
                base10 /= 64;
            } while (base10 != 0);

            for (int i = tmp.Length - 1; i >= 0; i--)
            {
                base64 += tmp[i];
            }
            return base64;
        }
    }
    //============== Load File Class ==========================//
    class IOFile
    {

        DictType dtype;
        public IOFile()
        {
            dtype = GetDefaultDictType();
        }
        public DictType DefaultDictType
        {
            get { return dtype; }
            set { dtype = value;
            SetDefaultDictType(dtype);
                }
        }
        private  DictType GetDefaultDictType()
        {
            
                string[] tmp = Read_DefaultDatabae().Split(';');
                return new DictType(Convert.ToBoolean(tmp[0]), tmp[1], tmp[2], tmp[3]);
            
        }
        private void SetDefaultDictType(DictType _dt)
        {
            ArrayOfDictType arofdt = GetAllDictType();
            StringBuilder strBuilder = new StringBuilder();
            string def = string.Empty;
            foreach (DictType dt in arofdt)
            {
                if (dt.DBWord == _dt.DBWord) 
                    def = "true;";
                else
                    def = "false;";
                 strBuilder.AppendLine(def + dt.DbName + ';' + dt.DbPath + ';' + dt.DBWord);
          
            }
            File.WriteAllText("Dicttype.txt",strBuilder.ToString());
        }
        public  string Read_DefaultDatabae()
        {
            string df = string.Empty;
            string[] dataString = File.ReadAllLines("Dicttype.txt");
            foreach (string line in dataString)
            {
                string[] ApartOfLine = line.Split(';');
                if (ApartOfLine[0] == "true")
                {
                    df = line;
                    break;
                }
            }
            return df;
        }
        public  ArrayOfDictType GetAllDictType()
        {
            string[] dataString = File.ReadAllLines("Dicttype.txt");
            ushort i = 0;
            Classes.ArrayOfDictType AllDictType = new Classes.ArrayOfDictType(dataString.Length);
            foreach (DictType ds in AllDictType)
            {
                string line = dataString[i++];
                    string[] apartofline = line.Split(';');
                    ds.IsDefault = Convert.ToBoolean(apartofline[0]);
                    ds.DbName = apartofline[1];
                    ds.DbPath = apartofline[2];
                    ds.DBWord = apartofline[3];
       
            }
            return AllDictType;

        }
        public void Write_DictType(string[] str)
        {
           
            File.WriteAllLines("dicttype.txt", str);            
        }
        public List<string> GetAllWordFromDictType(Classes.DictType dt)
        {
            List<string> l = new List<string>();
            StreamReader sr = new StreamReader(dt.DBWord);
            string line = string.Empty;
            while ((line = sr.ReadLine()) != null)
            {
                l.Add(line);
            }
            sr.Close();
            return l;
        }
        public bool WriteWordFileFromList(string filePath,List<string> l)
        {
            if (File.Exists(filePath))
            {

                File.WriteAllLines(filePath, l.ToArray());
                return true;
            }
            return false;
        }
        public bool AddWordToFile(string word,string filePath)
        {
            if (File.Exists(filePath))
            {
                FileStream fs = new FileStream(filePath, FileMode.Append, FileAccess.Write);
                StreamWriter sw = new StreamWriter(fs);
                sw.BaseStream.Seek(0, SeekOrigin.End);
                sw.WriteLine(word);
                sw.Flush();
                fs.Close();
                return true;
            }
            return false;
        }
            
    }
}
